﻿#include "Vines.h"
#include "cocos2d.h"
#include "cocos-ext.h"
#include "Config.h"

using namespace cocos2d;
using namespace cocos2d::extension;

Vines::Vines(Monkey* monkey)
{
	initValueForAttribute( VBIG, ccp(0,0), VISLEFT, monkey); 
}
Vines::Vines(int type, Monkey* monkey)
{
	initValueForAttribute( type, ccp(0,0), VISLEFT, monkey);
}
Vines::Vines(int type, CCPoint p, Monkey* monkey)
{
	initValueForAttribute( type, p, VISLEFT, monkey);
}
Vines::Vines(int type, CCPoint p, int left, Monkey* monkey)
{
	initValueForAttribute( type, p, left, monkey);
}

void Vines::initValueForAttribute(int type, CCPoint p, int left, Monkey* monkey)
{
	initWithFile(fSelectObjectImage());
	setAnchorPoint(ccp(0.5, 1));
	mType = type;
	setPosition(p);
	mIsLeft = left;
	mNewStatus = FSHOW;
	mCurrentStatus = FSHOW;
	mMonkey = monkey;
}

char* Vines::fSelectObjectImage()
{
	switch(mType)
	{
	case VBIG:
		return kImageVinesBig;
	case VSMALL:
		return kImageVinesSmall;
	case VWILTING:
		return kImageVinesWilting;
	default: 
		return kImageVinesBig;
	}
}

void Vines::fPreLoadMusic()
{
	CCLog("PreLoad music !!!!");
	//CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(kSoundBroken);
}

void Vines::update(float dt)
{	
	if (mCurrentStatus != mNewStatus) // trang thai thay doi
    {
        mCurrentStatus = mNewStatus;
        fEffect();
    }
}

void Vines::fEffect()
{
	switch(mCurrentStatus)
	{
			//action
			//play sound effect here

		case FSHOW: //hien
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case FHIDE: //an
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		case FCOLLISION: //va cham
			// CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect(kSoundBroken);
			break;

		default: 
			break;
	}
}

bool Vines::fCollisionWithMonkey(CCRect sp1, CCRect sp2)
{
	CCRect rectSp1 = sp1;
	CCRect rectSp2 = sp2;
	float minX1, minY1, maxX1, maxY1, minX2, minY2, maxX2, maxY2;
	float nScaleMonkey = mMonkey->fGetArmature()->getScaleX();
	CCPoint nPointMonkey = mMonkey->getPosition();

	//set tham so object 1
	minX1 = rectSp1.getMinX();
	minY1 = rectSp1.getMinY();
	maxX1 = rectSp1.getMaxX();
	maxY1 = rectSp1.getMaxY();

	//set tham so object 2
	minX2 = rectSp2.getMinX() * nScaleMonkey + nPointMonkey.x;
	minY2 = rectSp2.getMinY() + nPointMonkey.y;
	maxX2 = rectSp2.getMaxX() * nScaleMonkey + nPointMonkey.x;
	maxY2 = rectSp2.getMaxY() + nPointMonkey.y;

	return !(maxX1 < minX2 ||
	maxX2 < minX1 ||
	maxY1 < minY2 ||
	maxY2 < minY1);
}

void Vines::fupdateCollision(float delta)
{
	CCRect sp1 = this->boundingBox();
	CCRect sp2 = mMonkey->fGetArmature()->getBone("Layer6")->getDisplayRenderNode()->boundingBox();
	if(fCollisionWithMonkey( sp1, sp2))
	{
		mMonkey->fGetArmature()->getAnimation()->play("duday");
		
		//addChild(mMonkey->fGetArmature());
		mMonkey->setParent(this);
		
		//mMonkey->setPosition(ccp(getPositionX() + mMonkey->fGetArmature()->boundingBox().size.width/2 - 8 ,getPositionY() - getContentSize().height - mMonkey->fGetArmature()->getContentSize().height/2));
		
		//addChild(mMonkey);
		//

		//setRotation(100);
		CCRotateBy* nRotateTo = CCRotateBy::create(0.2f, -20);
		//runAction(CCSequence::createWithTwoActions(nRotateTo, CCCallFuncO::create(nRotateTo , callfuncO_selector(Vines::test) , (CCObject *) this) ));

		unschedule(schedule_selector(Vines::fupdateCollision));
	}
}

void Vines::fCheckStart()
{
	schedule(schedule_selector(Vines::fupdateCollision));
}